<template>
  <div class="login-container">
    <div class="loginBox">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <!-- 放置标题图片 @是设置的别名-->
        <div class="title-container">
          <h3 class="title titles">
            黑马面面用户登录
            <!-- <img src="@/assets/common/login-logo.png" alt="" /> -->
          </h3>
        </div>

        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="mobile"
            v-model="loginForm.username"
            class="logput"
            placeholder="请输入手机号"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            class="logput"
            :type="passwordType"
            placeholder="请输入密码"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>

        <!--
        .native -- 给子组件的根元素，绑定原生事件
                      也就是子组件的根元素没有绑定事件，没有子传父事件，我们可以在父组件中给引用的子组件添加.native修饰符来给子组件根元素，绑定原生事件
      -->
        <el-button
          class="loginBtn"
          :loading="loading"
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click.native.prevent="handleLogin"
        >
          登录
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import Shajs from 'jssha'
// import request from '@/utils/request'

export default {
  name: 'Login',
  data() {
    return {
      // 表单
      loginForm: {
        username: 'root@admin.com',
        password: '123456'
      },
      // 校验规则
      loginRules: {
        username: [
          {
            required: true,
            message: '请输入手机号',
            trigger: ['blur', 'change']
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: ['blur', 'change']
          },
          {
            min: 6,
            max: 16,
            message: '请输入6-16位的正确密码',
            trigger: ['blur', 'change']
          }
        ]
      },
      loading: false, // 登录按钮的加载状态
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    // ...mapActions(['user/getToken']), // 写法一
    ...mapActions('user', ['login']), // 写法二
    // 【显示密码】
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 【点击登录】
    handleLogin() {
      const shajs = new Shajs('SHA-256', 'TEXT', { encoding: 'UTF8' })
      // 2. 要加密的密码
      shajs.update(this.loginForm.password)
      // 3. 获取
      const password = shajs.getHash('HEX')
      const username = this.loginForm.username
      this.$refs.loginForm.validate((valid, val) => {
        if (valid) {
          // 点击登录按钮开启加载状态
          this.loading = true
          this.login({ username: username, password: password })
            .then(() => {
              // 成功后跳转页面
              this.$router.push('/')

              // 登录成功失败都关闭加载状态
              // this.loading = false
            })
            .catch(() => {
              // 失败的结果
              console.log(
                '3.此处调用的vuex中的getToken方法，走的又是失败方法reject -- 第三步'
              )
              // this.loading = false
            })
            .finally(() => {
              // 无论成功还是失败，都会执行finally方法 -- 关闭加载状态
              this.loading = false
            })
        } else {
          console.log(`表单里未满足规则的字段：${JSON.stringify(val)}`)
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #333; // 将输入框颜色改成蓝色
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    // color: $cursor;
    color: #333;
  }
}
.logput {
  color: #333 !important;
}
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
    border-radius: 5px;
    color: #454545;
  }

  .el-form-item__error {
    color: #fff;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #fff;
.loginBox {
  height: 100%;
  width: 100%;
  background: url('http://hmmm.itheima.net/img/logingBg.989212c0.png') no-repeat
    100% 100%;
  position: relative;
}
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: #2d3a4b;
  background-image: url('http://hmmm.itheima.net/img/bg.2f46505a.jpg'); // 设置背景图片
  background-size: 100% 100%;
  background-repeat: no-repeat;
  .login-form {
    position: absolute;
    left: 20%;
    top: 50%;
    width: 520px;
    padding: 35px 35px 15px 35px;
    margin: -200px 0 0 0;
    background: #f5f5f5;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .titles {
      color: #407ffe;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .loginBtn {
    border-radius: 15px;
    background: #407ffe;
    height: 50px;
    line-height: 20px;
    font-size: 22px;
  }
}
</style>
